﻿loadmenu();

function loadmenu() {
    $.ajax({
        type: "get",
        url: "/home/getmenu",
        async: false,
        success: function (menus) {
            console.log(menus)
            var tmphtml = initmenu(menus);
            console.log(tmphtml)
            $('#leftmenu').html(tmphtml);
        }
    })
}

function initmenu(menus) {
    var tmphtml = '';
    for (var i = 0; i < menus.length; i++) {
        if (menus[i].state) {
            if (i == 0) {
                tmphtml += '<li class="layui-nav-item layui-nav-itemed">';
            } else {
                tmphtml += '<li class="layui-nav-item">';
            }
            if (menus[i].url && menus[i].url !== '') {
                tmphtml += '<a class="site-active" style="cursor: pointer;" tab-url="' + menus[i].url + '" tab-name="' + menus[i].title + '" tab-layid="' + menus[i].id + '">' + menus[i].title + '</a>';
            } else {
                tmphtml += '<a class="site-active" style="cursor: pointer;">' + menus[i].title + '</a>';
            }
            var m = menus[i].childrens.length;
            if (m > 0) {
                tmphtml += '<dl class="layui-nav-child">';
                for (var j = 0; j < m; j++) {
                    if (menus[i].childrens[j].state) {
                        tmphtml += '<dd><a class="site-active" style="margin-left:20px" tab-name="' + menus[i].childrens[j].title + '" tab-Url="' + menus[i].childrens[j].url + '" tab-layid="' + menus[i].childrens[j].id + '">' + menus[i].childrens[j].title + '</a>';
                        if (menus[i].childrens[j].childrens != null) {
                            var n = menus[i].childrens[j].childrens.length;
                            if (n > 0) {
                                tmphtml += '<dl class="layui-nav-child">';
                                for (var x = 0; x < n; x++) {
                                    if (menus[i].childrens[j].childrens[x].state) {
                                        tmphtml += '<dd><a class="site-active" style="margin-left:40px" tab-name="' + menus[i].childrens[j].childrens[x].title + '" tab-Url="' + menus[i].childrens[j].childrens[x].url + '" tab-layid="' + menus[i].childrens[j].childrens[x].id + '">' + menus[i].childrens[j].childrens[x].title + '</a></dd>';
                                    }
                                }
                                tmphtml += '</dl>';
                            }
                            tmphtml += "</dd>";
                        }
                       
                    }
                }
                tmphtml += '</dl>';
            }
            tmphtml += '</li>';
        }
    }
    return tmphtml;
}
